﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LongQuanWeb.App_Code;

namespace LongQuanWeb.Admin
{
    public partial class Recruitment_InfoAddUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='招考信息'; </script>");
                string type = Request.QueryString["type"].ToString().Trim();
                if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string sql = "select * from [News] where NewsCode=" + id + "";
                    DataSet ds = DbHelperSQL.Query(sql);
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    {
                        this.txt_title.Text = ds.Tables[0].Rows[0]["Title"].ToString();
                        this.txt_source.Text = ds.Tables[0].Rows[0]["Author"].ToString();
                        this.txtContent.Value = ds.Tables[0].Rows[0]["NewsContent"].ToString();
                    }
                }
                btn_return.CommandArgument = type;
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            try
            {
                if (type == "1")
                {
                    string strSql = "insert into [News](Title,NewsContent,Author,CreateTime) "
                                  + "values('" + this.txt_title.Text.Trim() + "','" + this.txtContent.Value + "','" + this.txt_source.Text.Trim() + "','" + DateTime.Now.ToString() + "') ";
                    if (DbHelperSQL.ExecuteSql(strSql) > 0)
                    {
                        base.Response.Write("<script>alert('添加成功');window.location='Recruitment_InfoAddUpdate.aspx?type=1'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('添加失败');</script>");
                    }
                }
                else if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string sql = "Update [News] set Title='" + this.txt_title.Text.Trim() + "',NewsContent='" + this.txtContent.Value + "',Author='" + this.txt_source.Text.Trim() + "',CreateTime='" + DateTime.Now.ToString() + "' where NewsCode=" + id + "";
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        base.Response.Write("<script>alert('修改成功');window.location='Recruitment_Info.aspx'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('修改失败');</script>");
                    }
                }
            }
            catch (Exception er)
            {
                Response.Write(" <script defer>alert('" + er.Message + "') </script>");
            }
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            //if (type == "1")
            //{
            //    Response.Redirect("Venture_ElegantAddUpdate.aspx?type=1");
            //}
            //else 
            //if (type == "2")
            //{
            Response.Redirect("Recruitment_Info.aspx");
            //}
        }
    }
}
